#include "reversi_board.h"

ReversiBoard::ReversiBoard(int player_color)
{
    set_player_color(player_color);
    m_board.fill(false,64);
    m_score = 0;
    if(m_player_color == 0){
        set_piece(3,4,1);
        set_piece(4,3,1);
    }else{
        set_piece(3,3,1);
        set_piece(4,4,1);
    }

}

QBitArray ReversiBoard::get_board(){
    return m_board;
}

void ReversiBoard::print(){
    int piece;
    for(int row  = 0; row < 8; row++){
        for(int col = 0; col < 8 ; col++){
            piece = get_piece(row, col);
            cout <<  piece << " | ";
            if ( col == 7){
                cout << piece;
            }

        }
        cout << endl;

    }


}

void ReversiBoard::set_board(ReversiBoard *board){
    for(int i  = 0; i < 8; i++){
        for(int j = 0; j < 8 ; j++){
            this->set_piece(i,j,board->get_piece(i,j));
        }
    }

}


int * ReversiBoard::to_array(){
    int *array = (int*) malloc (64 * sizeof(int));
    for ( int row = 0; row < 8; row++){
        for ( int col = 0; col < 8; col++){
            array[8*row + col] = get_piece(row, col);
        }
    }
    return array;
}

void ReversiBoard::set_player_color(int player_color){
    this->m_player_color = player_color;
}

int ReversiBoard::get_player_color(){
    return m_player_color;
}

void ReversiBoard::set_score(int score){
    this->m_score = score;
}
//se tu vir algo estranho
void ReversiBoard::set_piece(int x, int y, bool value){
    if((x >= 0 && x < 8) && (y >= 0 && y < 8)){
       // m_board[8*x + y] = value;
        m_board.setBit(8*x + y,value);
        if(value){
            m_score++;
        }else{
            m_score--;
        }
    }
}

bool ReversiBoard::get_piece(int x, int y){
    if((x >= 0 && x < 8) && (y >= 0 && y < 8)){
        return m_board.testBit(8*x + y);
    }
    return 0;
}

unsigned int ReversiBoard::get_score(){
    return m_score;
}

/*ReversiBoard & ReversiBoard::operator =(const ReversiBoard &other){
    if ( this != &other ){
        ReversiBoard temp = ReversiBoard(other.get_player_color());
        for ( int i =0 ; i < 8 ; i++){
            for (int j = 0; j < 8; j++){
                temp.set_piece(i,j, other.get_piece(i, j));
            }
        }
        temp.set_score(other.get_score());
    }

    return *temp;
}*/
